package org.yxt.springclod.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * spring cloud网关配置
 *
 * @author Yang douya
 * @date 2022-01-04
 */
@Configuration
public class GatewayConfig {

    /**
     * 编码方式实现路由配置
     * 当访问 http://localhost:9527/guonei 转发到 http://news.baidu.com/guonei
     *
     * @param routeLocatorBuilder
     * @return
     */
    @Bean
    public RouteLocator consumerRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("path_route_guonei", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
        return routes.build();
    }

    /**
     * 编码方式实现路由配置
     * 当访问 http://localhost:9527/guoji 转发到 http://news.baidu.com/guoji
     *
     * @param routeLocatorBuilder
     * @return
     */
    @Bean
    public RouteLocator consumerRouteLocator2(RouteLocatorBuilder routeLocatorBuilder) {
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("path_route_guonei", r -> r.path("/guoji").uri("http://news.baidu.com/guoji")).build();
        return routes.build();
    }
}
